using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Utilities;
using Utilities.Data;
using Utilities.Network;
using CapstoneGame.Utilities.Graphics;

namespace CapstoneGame
{
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        SoundManager soundManager;
        GameLog log;
        Settings settings;
        ResolutionHandler resHandler;


        public static TitleScreen titleScreen;
        public static OptionsScreen optionsScreen;
        public static CreditsScreen creditsScreen;
        public static PlayingScreen playingScreen;
        public static LoadingScreen loadingScreen;
        public static OpeningScreen openingScreen;

        //use for time keeping
        public static long totalElapsedTime = 0;

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            resHandler = new ResolutionHandler(graphics, GraphicsAdapter.DefaultAdapter);
            Content.RootDirectory = "Content";
        }


        protected override void Initialize()
        {
            spriteBatch = new SpriteBatch(GraphicsDevice);
            soundManager = new SoundManager(this, Content);
            FileHandler fileHandler = new FileHandler();

            settings = fileHandler.LoadSettings();
            settings.SetDefaults();
            SetSettings();

            fileHandler.SaveSettings(settings);

            //set custom settings
            //TODO REMOVE THIS LATER
            settings.opening_screen_time = 0;
            settings.print_log = true;

            GameScreen.Settings = settings;

            log = new GameLog();
            GameScreen.Log = log;

            GameScreen.Graphics = graphics;
            GameScreen.SpriteBatch = spriteBatch;
            GameScreen.Content = Content;
            GameScreen.SoundManager = soundManager;
            GameScreen.FileHandler = fileHandler;
            GameScreen.ResolutionHandler = resHandler;


            titleScreen = new TitleScreen(this);
            playingScreen = new PlayingScreen(this);
            optionsScreen = new OptionsScreen(this);
            creditsScreen = new CreditsScreen(this);
            loadingScreen = new LoadingScreen(this);
            openingScreen = new OpeningScreen(this);

            Components.Add(titleScreen);
            Components.Add(optionsScreen);
            Components.Add(creditsScreen);
            Components.Add(playingScreen);
            Components.Add(loadingScreen);
            Components.Add(soundManager);
            Components.Add(openingScreen);
            //Components.Add(new GamerServicesComponent(this));

            GameScreen.SetScreenStatus(openingScreen, true);

            base.Initialize();
        }


        protected override void LoadContent()
        {

        }


        protected override void UnloadContent()
        {

        }


        protected override void Update(GameTime gameTime)
        {
            if (!playingScreen.Active)
            {
                IsMouseVisible = true;
            }

            totalElapsedTime += gameTime.ElapsedGameTime.Milliseconds;
            base.Update(gameTime);
        }


        protected override void Draw(GameTime gameTime)
        {
            base.Draw(gameTime);
        }

        #region Settings
        private void SetSettings()
        {
            Window.Title = Defaults.GAME_NAME;
            SetGraphics();
            SetGameSettings();
        }

        public void SetGameSettings()
        {
            this.IsFixedTimeStep = settings.is_fixed_time_step;
            this.TargetElapsedTime = settings.updates_per_second;
        }

        public void SetGraphics()
        {
            ResolutionHandler.Resolution res = new ResolutionHandler.Resolution();
            res.Width = (int)settings.window_width;
            res.Height = (int)settings.window_height;
            if (resHandler.isResolutionSupported(res))
                resHandler.setResolution(res);
            else
                resHandler.setMaxResolution(settings);

            graphics.IsFullScreen = settings.fullscreen;
            graphics.SynchronizeWithVerticalRetrace = settings.synchronize_with_vertical_retrace;
            graphics.PreferMultiSampling = settings.prefer_multi_sampling;
            graphics.ApplyChanges();
        }
        #endregion
    }
}
